﻿#define Status3//huangshenyu
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FortuneHeavenCommon.DataMap;
using System.Data.SqlClient;

namespace FortuneHeavan.Game.DAL
{
    public class RechargeCodeDAL : UserDBBase
    {
        /// <summary>
        /// 充值记录
        /// </summary>
        /// <param name="id">充值id</param>
        /// <param name="userid">用户id</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool Recharge(int id,int userid)
        {
            string sql = "update RechargeCodeRecord set UserID = @UserID, Status = 2 where ID = @ID";
            return RunSQLNonQuery(sql, new SqlParameter("@UserID", userid),new SqlParameter("@ID",id));
        }

        /// <summary>
        /// 将某一张卡的状态设置为可用
        /// </summary>
        /// <param name="id">需要生效的卡的ID号</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool Valid(int id)
        {
            return Valid(new List<int>{id} );
        }

        /// <summary>
        /// 卡生效
        /// </summary>
        /// <param name="list">需要生效的卡的ID号</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool Valid(IList<int> ids)
        {
            if (ids == null || ids.Count <= 0)
                return false;
            if (ids.Count <= 0)
            {
                return true;
            }
            string sql = "update RechargeCodeRecord set Status = 1 where ID in (";
            IList<SqlParameter> SqlParameters=new List<SqlParameter>();
            bool first = true;
            foreach (int ID in ids)
            {
                if (first)
                {
                    sql += "@ID" + ID;
                    first = false;
                }
                else
                {
                    sql += ",@ID" + ID;
                }
                SqlParameters.Add(new SqlParameter("@ID"+ID,ID));
            }
            sql += ')';
            return RunSQLNonQuery(sql, SqlParameters.ToArray());
        }

        public List<RechargeCodeRecord> GetList(int page, int size, int isUsed, int NetBarID)
        {
            string condition = "where 1=1  ";
#if Status3
            if(isUsed > -1)
            {
                condition += " and Status ="+isUsed;
            }

            if (NetBarID > 0)
            {
                condition += " and NetBarID =" + NetBarID;
            }
#else

            if (isUsed == 1)
            {
                condition += " and Status =1 ";
            }
            else
            {
                if (isUsed == 0)
                {
                    condition += " and Status !=1 ";
                }
            }
#endif
            string sql = @"SELECT * FROM (SELECT A.[ID],A.[RechargeCode],A.[RechargeValue],A.[Status],A.[CreateTime],A.[NetBarId],A.[UsedTime],A.UserID,B.Accounts as UserName ,ROW_NUMBER() OVER(ORDER BY A.ID DESC) ROW   
from  RechargeCodeRecord A left join AccountsInfo B on A.UserID =B.UserID  {0} 
 ) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1";
            List<RechargeCodeRecord> list = Query<RechargeCodeRecord>(string.Format(sql,condition), new SqlParameter("@p0", size * (page - 1) + 1), new SqlParameter("@p1", size * page));
            return list;
        }

        public int GetTotalSize(int isUsed, int NetBarID)
        {
            string sql;
            string condition = "where 1=1  ";
#if Status3
            if(isUsed > -1)
            {
                condition += " and Status ="+isUsed;
            }
            if (NetBarID > 0)
            {
                condition += " and NetBarID =" + NetBarID;
            }
#else

            if (isUsed == 1)
            {
                condition += " and Status =1 ";
            }
            else
            {
                if (isUsed == 0)
                {
                    condition += " and Status !=1 ";
                }
            }
#endif
            sql = @"select count(id) from RechargeCodeRecord {0}";
            return RunSQL(string.Format(sql,condition));

        }
        public RechargeCodeRecord GetRechargeCode(string code)
        {
            string sql = @"SELECT * FROM  RechargeCodeRecord where RechargeCode =@RechargeCode";
            List<RechargeCodeRecord> list = Query<RechargeCodeRecord>(sql, new SqlParameter("@RechargeCode", code));
            if (list != null && list.Count>0)
            {
                return list[0];
            }
            return null;
        }
    }
}